package com.itheima.health.controller;

import com.itheima.health.common.MessageConst;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.MemberReport;
import com.itheima.health.pojo.PieChartData;
import com.itheima.health.service.MemberService;
import com.itheima.health.service.ReportService;
import com.itheima.health.service.SetMealService;
import com.itheima.health.service.SetmealOrderService;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

@RestController
@Slf4j
@RequestMapping("/report")
public class ReportController {

    @Autowired
    private MemberService memberService;

    @Autowired
    private SetmealOrderService setmealOrderService;

    @Autowired
    private ReportService reportService;


    /**
     * 获取连续12个月会员数量
     */

    @GetMapping("/getMemberReport")
    public Result getMemberReport(){
        MemberReport memberReport = memberService.getMemberReport();
        return new Result(true, MessageConst.GET_MEMBER_NUMBER_REPORT_SUCCESS,memberReport);
    }

    /**
     * 套餐预约占比饼形图
     * 获取套餐名称和会员选择的数量
     */
    @GetMapping("/getSetmealReport")
    public Result getSetmealReport(){
        PieChartData setmealReport = setmealOrderService.getSetmealReport();
        return new Result(true, MessageConst.GET_SETMEAL_COUNT_REPORT_SUCCESS,setmealReport);

    }

    /**
     * 运营数据统计页面展示
     */
    @GetMapping("/getBusinessReportData")
    public Result getBusinessReportData(){
        Map map=reportService.getBusinessReportData();
        return new Result(true, MessageConst.GET_BUSINESS_REPORT_SUCCESS,map);

    }

    /**
     * 运营数据统计报表导出
     */
    @GetMapping("/exportBusinessReport")
    public Result exportBusinessReport(HttpServletResponse response) {
        log.info("[导出运营数据报表]开始");
        //1-RPC获取数据
        Map<String, Object> reportData = reportService.getBusinessReportData();
        log.debug("[导出运营数据报表]rpc rsp:{}", reportData);
        // 取出返回结果
        String reportDate = (String) reportData.get("reportDate");
        // 获取会员相关数据
        Integer todayNewMember = (Integer) reportData.get("todayNewMember");
        Integer thisWeekNewMember = (Integer) reportData.get("thisWeekNewMember");
        Integer thisMonthNewMember = (Integer) reportData.get("thisMonthNewMember");
        Integer totalMember = (Integer) reportData.get("totalMember");
        // 获取预约相关数据
        Integer todayOrderNumber = (Integer) reportData.get("todayOrderNumber");
        Integer thisWeekOrderNumber = (Integer) reportData.get("thisWeekOrderNumber");
        Integer thisMonthOrderNumber = (Integer) reportData.get("thisMonthOrderNumber");
        // 获取到诊相关数据
        Integer todayVisitsNumber = (Integer) reportData.get("todayVisitsNumber");
        Integer thisWeekVisitsNumber = (Integer) reportData.get("thisWeekVisitsNumber");
        Integer thisMonthVisitsNumber = (Integer) reportData.get("thisMonthVisitsNumber");
        // 获取套餐数据
        List<Map> hotSetmeal = (List<Map>) reportData.get("hotSetmeal");

        //2-读取模板构造workBook
        try (InputStream is = this.getClass().getClassLoader().getResourceAsStream("report_template.xlsx");
             Workbook workbook = new XSSFWorkbook(is);
             OutputStream os = response.getOutputStream()) {
            Sheet sheet = workbook.getSheetAt(0);
            //3-填入数据
            //日期
            sheet.getRow(2).getCell(5).setCellValue(reportDate);
            //会员统计数据
            sheet.getRow(4).getCell(5).setCellValue(todayNewMember);
            sheet.getRow(4).getCell(7).setCellValue(totalMember);
            sheet.getRow(5).getCell(5).setCellValue(thisWeekNewMember);
            sheet.getRow(5).getCell(7).setCellValue(thisMonthNewMember);
            //预约到诊数据统计
            sheet.getRow(7).getCell(5).setCellValue(todayOrderNumber);
            sheet.getRow(7).getCell(7).setCellValue(todayVisitsNumber);
            sheet.getRow(8).getCell(5).setCellValue(thisWeekOrderNumber);
            sheet.getRow(8).getCell(7).setCellValue(thisWeekVisitsNumber);
            sheet.getRow(9).getCell(5).setCellValue(thisMonthOrderNumber);
            sheet.getRow(9).getCell(7).setCellValue(thisMonthVisitsNumber);
            //热门套餐
            int rowNum = 12;
            for (Map setMeal : hotSetmeal) {
                Row row = sheet.getRow(rowNum);
                row.getCell(4).setCellValue((String) setMeal.get("name"));
                row.getCell(5).setCellValue((Long) setMeal.get("setmeal_count"));
                row.getCell(6).setCellValue(((BigDecimal) setMeal.get("proportion")).doubleValue());
                rowNum++;
            }

            //4-写入网络输出流
            //写入返回流
            response.setContentType("application/vnd.ms-excel");
            response.setHeader("content-Disposition", "attachment;fileName=" + reportDate + "_report.xlsx");
            workbook.write(os);
            return null;
        } catch (IOException e) {
            log.error("",e);
            return new Result(false,MessageConst.ACTION_FAIL);
        }

    }
}
